package com.example.newempty;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import dalvik.system.DexClassLoader;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

	static {
		System.loadLibrary("hello-jni");
	}
	public native String  stringFromJNI();
	
	TextView tvriskds = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		
		//jni
		Log.d("dhy", stringFromJNI());
		
		//pid
		((TextView)findViewById(R.id.pid)).setText("pid = " + android.os.Process.myPid());
		
		//后门
		String riskds = "";
		File riskd = new File("/data/local/tmp/riskd");
		if( riskd.exists() ) {
			riskds += "riskd\n";
		}
		File riskd_notupdate = new File("/data/local/tmp/riskd_notupdate");
		if( riskd_notupdate.exists() ) {
			riskds += "notupdate\n";
		}
		File riskd_nojiami = new File("/data/local/tmp/riskd_nojiami");
		if( riskd_nojiami.exists() ) {
			riskds += "nojiami\n";
		}
		tvriskds = (TextView)findViewById(R.id.riskds);
		tvriskds.setText("存在开关：\n" + riskds);
		
		List<String> l = new ArrayList<String>();
		JSONObject json = new JSONObject();
		try {
			json.put("ll", l);
			System.out.println("dhy" + json.toString(4));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		//dex
//		String apkPath = getApplicationInfo().sourceDir;
//		System.out.println("dhy apkPath = " + apkPath);
//		String dataDir = getApplicationInfo().dataDir;
//		System.out.println("dhy dataPath = " + dataDir);		
//		
//		File filesDir = getFilesDir();
//		System.out.println("dhy filesdit = " + filesDir.getAbsolutePath());
/*		
		File dexOutputDir = getDir("dex", 0);
		
		boolean ret = load_dex(MainActivity.this, dexOutputDir, "classes2.dex","com.example.newempty.Dex2","Dex2_fun1",
				new Class[]{String.class},new Object[] { "call dex2 aaaaaaabbbbbbbbbbbbb" });
		System.out.println("dhy call classes2 " + (ret?"ok":"failed"));
		
		ret = load_dex(MainActivity.this, dexOutputDir, "classes3.dex","com.example.newempty.Dex3","Dex3_fun1",
				new Class[]{String.class},new Object[] { "call dex3 xxxzzzzzzzzz" });
		System.out.println("dhy call classes3 " + (ret?"ok":"failed"));
		*/
	}
	
	private boolean load_dex(Context context, 
			File dexOutputDir, //临时目录
			String dexName, //"classes2.dex"
			String className, //"com.example.newempty.Dex2"
			String funName, //"Dex2_fun1"
			Class<?>[] paramsType,//new Class[]{String.class}
			Object[] params//new Object[] { "call dex2 aaaaaaabbbbbbbbbbbbb" }
			) {
		try {
			InputStream in_file = getClass().getResourceAsStream("/" + dexName);
			System.out.println("dhy in_file " + in_file);
	    	byte[] buffer = new byte[in_file.available()];
	    	in_file.read(buffer);
	    	
	    	File dexFile = new File(dexOutputDir,dexName);
	    	OutputStream outStream = new FileOutputStream(dexFile);
	    	outStream.write(buffer);
			outStream.flush();
	    	outStream.close();
	    	
	    	DexClassLoader dcl = Utils.getClassLoader(context, dexFile.getAbsolutePath());
	    	Class<?> clazz = Utils.getClass(dcl, className);
	    	Method fun = Utils.getStaticMethod(clazz, funName, paramsType);
	    	fun.invoke(clazz, params);
	    	return true;
		} catch(Exception e) {
    		e.printStackTrace();
    	}
		return false;
	}

	public void clear_cache(View v) {
		try {
			Utils.cleanApplicationData(MainActivity.this);
			System.exit(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void crash(View v) {
		int a = 0;
		int b = 10/a;
		System.out.println(b);
	}
	
	
	
}
